package com.moss.street.market;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * A class providing some date utilities used by the AdminAddBooking.jsp.
 * @author drusk
 * 
 */
public class Utilities {

	// TODO:  Have flexible start date so past Market Days can
	// be generated.
	/**
	 * Provides an array of market dates (all Saturdays) for the next year.
	 * @param dbformat
	 * @return String array of dates in the format yyyy-MM-dd or
	 *  EEE dd-MMM-yyyy depending on whether or not dbformat is true.
	 */
	public static String[] getMarketDays(boolean dbformat) {
		String sb[] = null;
		
		Calendar c = Calendar.getInstance();
		
		// Set the calendar to SATURDAY of the current week
		c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);

		DateFormat df = null;
		if (dbformat) {
			df = new SimpleDateFormat("yyyy-MM-dd");
		} else {
			df = new SimpleDateFormat("EEE dd-MMM-yyyy");
		}

		// Print upcoming Saturdays starting with current week
		// for the next year
		for (int i = 0; i <= 52; i++) {
			sb[i] = df.format(c.getTime());
			c.add(Calendar.DATE, 7);
		}
		return sb;
	}
	
	/**
	 * Used by AdminAddBooking to generate the current date
	 * @return Date in string format yyyy-MM-dd suitable for
	 * insertion into the database.
	 */
	public static String timeNow() {
		String time = null;
		
		Calendar c = Calendar.getInstance();
        Date now = new Date();
        c.setTime(now);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        time = df.format(c.getTime());
        
		return time;
	}
}
